package script.functions

import com.tang.script.functions.ScriptFunction
import com.tang.script.variable.VariablesContainer

@ScriptFunction(functionName = "出现重复")
static boolean duplicate(Object... lists) {
    if (lists.length <= 0) return false
    def lists1 = new LinkedList<List>()
    for (int i = 0; i < lists.length; i++) {
        if (lists[i] instanceof List) {
            lists1.add(lists[i]);
        }
    }

    int size = 0
    for (int i = 0; i< lists1.size(); i++) {
        size = Math.max(size, lists1.get(i).size())
    }
    if (lists1.isEmpty() || size <= 1) return false
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (compare(lists1, i, j))
                return true
        }
    }

    return false
}

//如果都是空的对象, 那么认为相等
//只要有一个字段不相等, 那就返回false, 否则返回true
static def boolean compare(LinkedList<List> lists, int index1, int index2) {
    for (int i = 0; i < lists.size(); i++) {
        List list = lists.get(i)
        Object v1 = getValue(list, index1)
        Object v2 = getValue(list, index2)
        if (!isEqual(v1, v2))
            return false
    }

    return true
}

static def Object getValue(List list, int index) {
    if (list == null || list.isEmpty() || index >= list.size()) return null
    return list.get(index)
}

static boolean isEqual(Object o1, Object o2) {
    if (o1 == null &&  o2 == null) return true
    if (o1 != null && o2 != null) return o1.equals(o2)
    return false
}
